/**
 * 
 */
package com.honghao.student.xml;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author Xiaohao
 *
 */
public class XStreamSampleAnnotation {
	private static XStream xStream;
	
	static{
		xStream = new XStream(new DomDriver());
		xStream.autodetectAnnotations(true);
	}
	
	public static User getUser(){
		UserLogs log = new UserLogs();
		log.setIp("192.168.1.91");
		log.setLoginDate(new Date());
		User user = new User();
		user.setUserId(1);
		user.setUserName("xstream");
		user.setLogs(log);
		return user;
	}
	
	public static void objectToXml() throws Exception{
		User user = getUser();
		FileOutputStream outputStream = new FileOutputStream("C:\\XStreamSample.xml");
		OutputStreamWriter writer = new OutputStreamWriter(outputStream);
		xStream.toXML(user, writer);
	}
}
